using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ListBox : MonoBehaviour {

	GameObject Label;
	GameObject ParentLabels;

    UILabel[] SelLabels;

	//ScrollItems
	GameObject ScrollBar;
	GameObject Background;
	GameObject Foreground;
	//end

	bool IsSelected = false;

	public string SelectedLabel;

	public UIFont font;
	public UIAtlas atlas;

	public float indent = 20.0f;
	public Vector2 CllipSize;
	public Vector2 CllipPosition;

	private Vector3 listPosition;

	int[] Maindata;
	string[] data;
	bool created = false;

	public int MaxPerline;

	public Color Select;
	public Color Instance;

	private Quaternion rotation;

	public enum ListArientation
	{
		vertical,
		horizontal,
		none
	}

	private ListArientation arientation;
	private ListArientation scrolldirect;

	// Use this for initialization

	public bool search(string FindThis)
	{
				for (int i = 0; i < data.Length; i++) 
				{
						if (data [i] == FindThis)
								return true;
				}
				return false;
	}

	void sorting(int[] array)
	{
				
				for (int i = 0; i <array.Length; i++) {
					for (int j = 0; j < array.Length; j++) {
								if (array [i] < array [j]) 
								{
										int h = array [i];
										array [i] = array [j];
										array [j] = h;
								}
					}
				}
				string o = "";
				for (int i = 0; i < array.Length; i++) {
						o += array[i].ToString() + " ";
				}
				Debug.Log (o);
	}
	void sorting(string[] array)
	{
				Array.Sort<string> (array);
		string o = "";
		for (int i = 0; i < array.Length; i++) {
			o += array[i].ToString() + " ";
		}
		Debug.Log (o);
	}

	void Sort()
	{
				DestroyList (ParentLabels);
				sorting (data);
		createAList (MaxPerline,data,arientation,scrolldirect,listPosition.x,listPosition.y,listPosition.z, rotation);
	}
	string selection(UILabel labl)
	{
				int count = ParentLabels.GetComponentsInChildren<UILabel> ().Length;
				SelLabels = new UILabel[count];
				SelLabels = ParentLabels.GetComponentsInChildren<UILabel> ();
				for (int i = 0; i < count; i++) 
				{
						SelLabels [i].color = new Color (Instance.r,Instance.g,Instance.b);
				}
				for (int i = 0; i < count; i++) 
				{
					if(SelLabels[i] == labl)
					{
								SelLabels [i].color = new Color (Select.r,Select.g,Select.b);
								SelectedLabel = SelLabels [i].text;
					}
				}
				IsSelected = true;
				return  SelectedLabel;
	}

	void OnClick()
	{
				if (UICamera.currentTouch.current.GetComponent<UILabel> () != null && UICamera.currentTouch.current.name == "ListData") {
						if (UICamera.currentTouch.current.GetComponent<UILabel> ().text == SelectedLabel) {
								UICamera.currentTouch.current.GetComponent<UILabel> ().color = new Color (Instance.r, Instance.g, Instance.b);
								SelectedLabel = "";
						} else
								selection (UICamera.currentTouch.current.GetComponent<UILabel> ());
				} else
						return;
		 
	}

	void Start ()
		{
				UICamera.genericEventHandler = this.gameObject;
		}

	void AddElements(string[] Elements)
	{
				DestroyList (ParentLabels);
				int add = Elements.Length;
				int f = data.Length;
				string[] SaveData = new string[data.Length];
				for (int i = 0; i < data.Length; i++) 
				{
						SaveData [i] = data [i];
				}
				f += add;
				data = new string[f];
				for (int i = 0; i < SaveData.Length; i++) 
				{
						data[i] = SaveData [i];
				}
				for (int i = SaveData.Length; i < data.Length; i++) 
				{
						data [i] = Elements [i - SaveData.Length];
				}
		createAList (MaxPerline,data,arientation,scrolldirect,listPosition.x,listPosition.y,listPosition.z, rotation);
	}

	void AddElements(string[] Elements, int index)
	{
		DestroyList (ParentLabels);
		int add = Elements.Length;
				//Debug.Log (add);
		int f = data.Length;
		string[] Save1 = new string[data.Length];
		string[] Save2 = new string[data.Length];
		if (index < 0)
			index = 0;
				if (index > (f))
						index = f;
		for (int i = 0; i < index; i++) 
		{
			Save1 [i] = data [i];
		}
				for (int i = index; i < data.Length; i++) 
				{
						Save2 [i - index] = data [i]; 
				}
		f += add;
				//Debug.Log (add);		
		data = new string[f];
		for (int i = 0; i < index; i++) 
		{
			data[i] = Save1 [i];
		}
				for (int i = index; i < add+index; i++) 
				{
						data [i] = Elements [i - index];
				}
		for (int i = index + add; i < data.Length; i++) 
		{
			data[i] = Save2[i - index - add];
		}
		createAList (MaxPerline,data,arientation,scrolldirect,listPosition.x,listPosition.y,listPosition.z, rotation);
	}

	public void DestroyList(GameObject ListParent)
	{
				Destroy (ListParent);
				Destroy (ScrollBar);
				created = false;
	}

	public void DleteElement(int index)
	{
				DestroyList (ParentLabels);
				string[] Save1 = new string[data.Length];
				string[] Save2 = new string[data.Length];
				int f = 0;
				for (int i = 0; i < index; i++) 
				{
						Save1[i] = data [i];
				}
				for (int i = index; i < data.Length; i++) 
				{
						Save2[i - index] = data [i];
				}
				
				f = data.Length;
				f--;
				data = new string[f];
				for (int i = 0; i < index-1; i++) 
				{
					data [i] = Save1[i];
				}
		//if (index == data.Length)
		//	index++;
				for (int i = index-1; i < data.Length; i++) 
				{
					data [i] = Save2[i - index+1];
				}
				//Debug.Log (created);
				if(!created)
			createAList (MaxPerline,data,arientation,scrolldirect,listPosition.x,listPosition.y,listPosition.z, rotation);
	}

	public void AddElement(string Element)
	{
				int f = 0;
				string[] DataSave = new string[data.Length];
		for(int i = 0; i < data.Length; i++)
		{
			DataSave [i] = data [i-3];
		}
				f = data.Length;
				f++;
				//Debug.Log (f);
		data = new string[f];
		for(int i = 0; i < DataSave.Length; i++)
		{
			data [i] = DataSave [i];
		}
				data.SetValue (Element,f-1);
	}

	public void AddElement(string Element, int index)
	{
				//index--;
		int f = 0;
		f = data.Length;
		f++;
		string[] Save1 = new string[data.Length];
		string[] Save2 = new string[data.Length];
		if (index < 0)
			index = 0;
		if (index > (f - 1))
			index = f - 1;
		for(int i = 0; i < index; i++)
		{
			Save1 [i] = data [i];
		}
				
		for(int i = index; i < data.Length; i++)
		{
			Save2 [i - index] = data [i];
		}
				//Save1 [index + 1] = Element;
				DestroyList (ParentLabels);
		data = new string[f];
		for(int i = 0; i < index; i++)
		{
			data [i] = Save1 [i];
		}
		data.SetValue (Element,index);
		//Debug.Log (data.Length);
		for(int i = index+1; i < data.Length; i++)
		{
						data [i] = Save2 [i - index-1];
		}
		string o = "";
				for (int i = 0; i < data.Length; i++) {
						o += data [i];
				}
				//Debug.Log (o);
		if(!created)
				createAList (MaxPerline,data,arientation,scrolldirect,listPosition.x,listPosition.y,listPosition.z, rotation);
	}

	private void CreateAScrollBar(float x, float y, float z)
	{
				ScrollBar = new GameObject ("scrolBar");
				ScrollBar.transform.parent = gameObject.transform;
				ScrollBar.transform.localPosition = new Vector3 (x+15.0f,y,0.0f);
				ScrollBar.transform.localScale = new Vector3 (1.0f,1.0f,1.0f);
				ScrollBar.AddComponent<UIScrollBar> ();
				ScrollBar.GetComponent<UIScrollBar> ().barSize = 0.118f;
				var face = ScrollBar.GetComponent<UIScrollBar> ();
				Background = new GameObject ("Background");
				Foreground = new GameObject ("Foreground");
				Background.AddComponent<UISprite> ();
				Background.AddComponent<BoxCollider> ();
				Background.AddComponent<UIEventListener> ();
				Foreground.AddComponent<UISprite> ();
				Foreground.AddComponent<BoxCollider> ();
				Foreground.AddComponent<UIEventListener> ();
				var back = Background.GetComponent<UISprite> ();
				var fore = Foreground.GetComponent<UISprite> ();
				back.type = UISprite.Type.Sliced;
				fore.type = UISprite.Type.Sliced;
				back.atlas = atlas;
				fore.atlas = atlas;
				back.spriteName = "Dark";
				fore.spriteName = "Light";
				face.background = back;
				face.foreground = fore;
				Background.transform.parent = ScrollBar.transform;
				Background.transform.localScale = new Vector3 (CllipSize.x, 22.0f, 1.0f);
				Foreground.transform.parent = ScrollBar.transform;
				Foreground.transform.localScale = new Vector3 (124.0f, 18.0f, 1.0f);			
	}

	public void createAList(int RowCount, string[] Data, ListArientation Arient, ListArientation ScrollDirect,float x,float y,float z, Quaternion rot)
	{
				arientation = Arient;
				scrolldirect = ScrollDirect;
				MaxPerline = RowCount;
				data = Data;
				rotation = rot;
				listPosition = new Vector3 (x,y,z);
		ParentLabels = new GameObject ("ListBox");
		ParentLabels.transform.parent = gameObject.transform;
		ParentLabels.transform.localScale = new Vector3 (gameObject.transform.localScale.x,gameObject.transform.localScale.y,1.0f);
				ParentLabels.transform.localPosition = new Vector3 (x,y,z);
				ParentLabels.transform.rotation = rot;
		ParentLabels.AddComponent<UIPanel> ();
		ParentLabels.AddComponent<UIDraggablePanel>();
				ParentLabels.GetComponent<UIDraggablePanel> ().repositionClipping = true;
		var l = ParentLabels.GetComponent<UIPanel>();
				l.clipping = UIDrawCall.Clipping.HardClip;
				l.clipRange = new Vector4 (CllipPosition.x,CllipPosition.y,CllipSize.x,CllipSize.y);
		CreateAlotOfLabels (data, 20.0f, 0.0f,RowCount,Arient);
		switch (ScrollDirect) 
		{
				case ListArientation.horizontal:
			CreateAScrollBar (-ParentLabels.GetComponent<UIPanel> ().clipRange.z, ParentLabels.GetComponent<UIPanel> ().clipRange.w, 0.0f);
						ParentLabels.GetComponent<UIDraggablePanel> ().horizontalScrollBar = ScrollBar.GetComponent<UIScrollBar> ();
						ScrollBar.GetComponent<UIScrollBar> ().direction = UIScrollBar.Direction.Horizontal;
						ParentLabels.GetComponent<UIDraggablePanel> ().scale = new Vector3 (1.0f, 0.0f, 0.0f);
			break;
				case ListArientation.vertical:
			Debug.Log (ParentLabels.GetComponent<UIPanel> ().clipRange.z);
						CreateAScrollBar (ParentLabels.GetComponent<UIPanel> ().clipRange.z, 0.0f, 0.0f);
						ParentLabels.GetComponent<UIDraggablePanel> ().verticalScrollBar = ScrollBar.GetComponent<UIScrollBar> ();
						ScrollBar.GetComponent<UIScrollBar> ().direction = UIScrollBar.Direction.Vertical;
						ParentLabels.GetComponent<UIDraggablePanel> ().scale = new Vector3 (0.0f, 1.0f, 0.0f);
			break;
			case ListArientation.none:
			ParentLabels.GetComponent<UIDraggablePanel> ().scale = new Vector3 (0.0f, 0.0f, 0.0f);
			break;
		}
				created = true;
	}
	public void createAList(int RowCount, string[] Data, ListArientation Arient,float x,float y,float z)
	{
		arientation = Arient;
		MaxPerline = RowCount;
		data = Data;
		listPosition = new Vector3 (x,y,z);
		ParentLabels = new GameObject ("ListBox");
		ParentLabels.transform.parent = gameObject.transform;
		ParentLabels.transform.localScale = new Vector3 (gameObject.transform.localScale.x,gameObject.transform.localScale.y,1.0f);
		ParentLabels.transform.localPosition = new Vector3 (x,y,z);
		ParentLabels.AddComponent<UIPanel> ();
		CreateAlotOfLabels (data, 20.0f, 0.0f,RowCount,Arient);
	}
	public void createLabel(string text, GameObject parent, float x, float y, float xpoz, float ypoz)
	{
				float rr = 0.0f;
				rr = UnityEngine.Random.Range (0.2f, 0.9f);
				float gg;
				gg = UnityEngine.Random.Range (0.2f, 0.9f);
				float bb;
				bb = UnityEngine.Random.Range (0.2f, 0.9f);
				Label = new GameObject ("ListData");
				Label.AddComponent<UILabel> ();
				Label.AddComponent<UIDragPanelContents> ();
				Label.GetComponent<UILabel> ().color = new Color (Instance.r, Instance.g, Instance.b);
				Label.GetComponent<UILabel> ().text = text;
				
				Label.transform.parent = parent.transform;
				Label.GetComponent<UILabel> ().font = font;
				Label.transform.localScale = new Vector2 (x,y);
				Label.transform.localPosition = new Vector2 (xpoz, ypoz);
				NGUITools.AddWidgetCollider (Label);
	}
	
	public void createLabel(string text,GameObject parent,Vector2 scale,Vector2 position)
	{ 
		float rr = 0.0f;
		rr = UnityEngine.Random.Range (0.2f, 0.9f);
		float gg;
		gg = UnityEngine.Random.Range (0.2f, 0.9f);
		float bb;
		bb = UnityEngine.Random.Range (0.2f, 0.9f);
		Label = new GameObject ("ListData");
		Label.AddComponent<UILabel> ();
		Label.AddComponent<UIDragPanelContents> ();

		Label.GetComponent<UILabel> ().text = text;
		Label.transform.parent = parent.transform;
		Label.GetComponent<UILabel> ().font = font;
		Label.transform.localScale = new Vector2 (scale.x,scale.y);
				Label.transform.localPosition = new Vector2 (position.x, position.y);
				NGUITools.AddWidgetCollider (Label);
	}

	public void CreateAlotOfLabels(string[] DataList)
	{
				float dy = 0.0f;
		if (DataList.Length == 0)	return;

				for (int i = 0; i < DataList.GetLength(0); i++) 
		{
						createLabel (DataList[i],GameObject.Find("Panel"),20.0f,20.0f,0.0f,dy);	
						dy -= indent;
		}
	}

	public void CreateAlotOfLabels(string[] DataList, float dx ,float dy ,int max,ListArientation Arient)
	{ 
		int i1 = 0;
		switch (Arient) 
		{
				case ListArientation.vertical:
			for (int i = 0; i < DataList.Length; i++) 
			{
				for (int j = 0; j < max; j++) 
				{
					if (i1 == DataList.Length || i1 > DataList.Length) 
					{
						//Debug.Log (i1);
						break;
					}
					createLabel (DataList [i1], ParentLabels, 20.0f, 20.0f, dx, dy);	
					dy -= indent;
					++i1;
				}
				//Debug.Log (i1);
				if (i1 == DataList.Length || i1 > DataList.Length) 
				{
					//Debug.Log (i1);
					break;
				}
				dy = 0;
				dx += 20 + indent;
			}
						break;
				case ListArientation.horizontal:
						dx = 0;
			for (int i = 0; i < DataList.Length; i++) 
			{
				for (int j = 0; j < max; j++) 
				{
					if (i1 == DataList.Length || i1 > DataList.Length) 
					{
						break;
					}
					createLabel (DataList [i1], ParentLabels, 20.0f, 20.0f, dx, dy);	
					dx += indent;
					++i1;
				}
				//Debug.Log (i1);
				if (i1 == DataList.Length || i1 > DataList.Length) 
				{
					//Debug.Log (i1);
					break;
				}
				dy -= 20 - indent;
				dx = 0;
			}
						break;
				default:
						break;
		}
	}
	public void CreateAlotOfLabels(string Data, int index , int count)
	{
		float dy = 0.0f;
		if (count == 0)	return;

		for (index = 0; index < count; index++) 
		{
			createLabel (Data,ParentLabels,20.0f,20.0f,0.0f,dy);	
			dy -= indent;
		}
	}
}
